//
//  YKShowPictureViewController.m
//  baisi
//
//  Created by 吴高烽 on 16/10/11.
//  Copyright © 2016年 吴高烽. All rights reserved.
//

#import "YKShowPictureViewController.h"
#import <UIImageView+WebCache.h>
#import "YKProgressView.h"
#import <SVProgressHUD.h>
@interface YKShowPictureViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic ,weak)  UIImageView *imageView;
@property (weak, nonatomic) IBOutlet YKProgressView *progressView;
@end

@implementation YKShowPictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.userInteractionEnabled = YES ;
  //  imageView.backgroundColor = [UIColor brownColor];
    [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backViewController:)]];
    [self.scrollView addSubview:imageView];
    self.imageView = imageView ;

    // 图片尺寸
    CGFloat pictureW = SCWidth;
    CGFloat pictureH = pictureW * self.topic.height / self.topic.width;
  //   imageView.center = CGPointMake(SCWidth * 0.5, SCHeight * 0.5) ;
    imageView.frame = CGRectMake(0, 0, pictureW, pictureH);
    if (pictureH > SCHeight) { // 图片显示高度超过一个屏幕, 需要滚动查看
        self.scrollView.contentSize = CGSizeMake(0, pictureH);
    } else {
        imageView.centerY = SCHeight * 0.5;
    }
 
    // 马上显示当前图片的下载进度
    [self.progressView setProgress:self.topic.pictureProgress animated:YES];
    // 下载图片,URL最为key对应operation对象，同一个URL只会下载一次。
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.topic.large_image] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        [self.progressView setProgress:1.0 * receivedSize / expectedSize animated:NO];
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.progressView.hidden = YES;
    }];
}
-(IBAction)backViewController:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)saveImage:(id)sender {
    UIImage *image = self.imageView.image;
    if (image == nil) {
        [SVProgressHUD showErrorWithStatus:@"图片还没下载完"];
        return ;
    }
    // 将图片写入相册，需要在info中设置权限
    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (error) {
        [SVProgressHUD showErrorWithStatus:@"保存失败"];
    }else{
        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
